package com.fh.modules.oms;

import com.fh.common.exception.Assert;

import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @author： qiujunda
 * @date： 2019/10/13 23:17
 */
public class OrderStatusValidator {

    public OrderStatus source;

    public OrderStatus target;

    public OrderStatusValidator(int source, OrderStatus target){
        this.source=getOrderStatus(source);
        this.target=target;
    }

    public void validate(){
        Map<OrderStatus, Set<OrderStatus>> statusMap = OrderStatus.STATUS_PROCESS;
        Set<OrderStatus> targets=statusMap.get(this.source);
        Assert.isTrue(targets==null||targets.size()==0,this.source.getName()+"订单拒绝操作");
        Assert.isFalse(targets.contains(this.target),this.source.getName()+"订单无法转换为"+this.target.getName());
    }

    public OrderStatus getOrderStatus(int orderStatusValue){
        Object[] enums = OrderStatus.class.getEnumConstants();
        for (Object anEnum : enums) {
            OrderStatus orderStatus=(OrderStatus)anEnum;
            if(orderStatus.getValue()==orderStatusValue){
                return orderStatus;
            }
        }
        Assert.isTrue(true,"订单状态无效");
        return null;
    }

}
